import { $parent } from '../function/index.js'
import test from '../function/test.js'

export default {
    props: {
        customStyle: {
            type: [Object, String],
            default: () => ({})
        },
        customClass: {
            type: String,
            default: ''
        },
        url: {
            type: String,
            default: ''
        },
        linkType: {
            type: String,
            default: 'navigateTo'
        }
    },
    data() {
        return {}
    },
    onLoad() {
    },
    created() {
    },
    computed: {
        /**
         * 生成bem规则类名
         * 由于微信小程序，H5，nvue之间绑定class的差异，无法通过:class="[bem()]"的形式进行同用
         * 故采用如下折中做法，最后返回的是数组（一般平台）或字符串（支付宝和字节跳动平台），类似['a', 'b', 'c']或'a b c'的形式
         * @param {String} name 组件名称
         * @param {Array} fixed 一直会存在的类名
         * @param {Array} change 会根据变量值为true或者false而出现或者隐藏的类名
         * @returns {Array|string}
         */
        bem() {
            return function (name, fixed, change) {
                // 类名前缀
                const prefix = `next-${name}--`
                const classes = {}
                if (fixed) {
                    fixed.map((item) => {
                        classes[prefix + this[item]] = true
                    })
                }
                if (change) {
                    change.map((item) => {
                        this[item] ? (classes[prefix + item] = this[item]) : (delete classes[prefix + item])
                    })
                }
                return Object.keys(classes)
                    // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-LARK
                    .join(' ')
                    // #endif
            }
        }
    },
    methods: {
        getParentData(parentName = '') {
            if (!this.parent) this.parent = {}
            this.parent = $parent.call(this, parentName)
            if (this.parent.children) {
                this.parent.children.indexOf(this) === -1 && this.parent.children.push(this)
            }
            if (this.parent && this.parentData) {
                Object.keys(this.parentData).map((key) => {
                    this.parentData[key] = this.parent[key]
                })
            }
        },
        preventEvent(e) {
            e && typeof (e.stopPropagation) === 'function' && e.stopPropagation()
        },
        noop(e) {
            this.preventEvent(e)
        }
    },
    onReachBottom() {
        uni.$emit('uOnReachBottom')
    },
    beforeDestroy() {
        if (this.parent && test.array(this.parent.children)) {
            const childrenList = this.parent.children
            childrenList.map((child, index) => {
                if (child === this) {
                    childrenList.splice(index, 1)
                }
            })
        }
    }
}
